package org.shunfeng.controller;

import org.shunfeng.dao.DeliveryListDao;
import org.shunfeng.dao.impl.DeliveryListDaoImpl;
import org.shunfeng.pojo.SysUser;
import org.shunfeng.service.DeliveryListService;
import org.shunfeng.service.UserService;
import org.shunfeng.service.impl.DeliveryListServiceImpl;
import org.shunfeng.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;

@WebServlet("/add/delivery")
public class AddDelivery extends HttpServlet {
    private DeliveryListService deliveryListService = new DeliveryListServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            req.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
            SysUser sysUser = (SysUser) req.getSession().getAttribute("user");
            Integer user_id = sysUser.getId();
            System.out.println("user_id = " + user_id);
            Map<String, String[]> parameterMap = req.getParameterMap();
    //        parameterMap.forEach((k, values) -> {
    //            for (String value : values) {
    //                System.out.println("Value: " + value);
    //            }
    //            System.out.println("*********" + values[0]);
    //            }
    //        );
    //        for (Map.Entry<String, String[]> entry : parameterMap.entrySet()){
    //            System.out.println("entry.getKey() = " + entry.getKey());
    //            System.out.println("entry.getValue() = " + entry.getValue()[0]);
    //        }

            int count = deliveryListService.addDelivery(user_id, parameterMap);
            if ( count > 0 ) {
                resp.sendRedirect(req.getContextPath() + "/delivery/list");
//                req.getRequestDispatcher("/delivery/delivery/list").forward(req, resp);
            }else {
//                req.getRequestDispatcher("/delivery/toAddDevlivery").forward(req, resp);
                resp.sendRedirect(req.getContextPath() + "/delivery/toAddDevlivery");
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


    }
}
